package org.mapki;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.location.Location;
import android.util.Log;

import com.google.android.maps.GeoPoint;

public class GpxHandler extends DefaultHandler {

	//Boolean currentElement = false;
	//String currentValue = null;
	private List<GeoPoint> gList = new ArrayList<GeoPoint>();
	private Location prevLocation = new Location("prevLocation");
	private Location currLocation = new Location("currLocation");
	private static float MIN_DISTANCE = 100f;
	
	public List<GeoPoint> getGeoPoints() {
		return gList;
	}

	/**
	 * Called when tag starts ( ex:- <name>AndroidPeople</name> -- <name> )
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		//currentElement = true;
		if (localName.equals("trkpt")) {
			String lat = attributes.getValue("lat");
			String lon = attributes.getValue("lon");
			Double dLat = Double.parseDouble(lat);
			Double dLon = Double.parseDouble(lon);
			currLocation.setLatitude(dLat);
			currLocation.setLongitude(dLon);
			float distance = currLocation.distanceTo(prevLocation);
			//Log.i("GpxHandler", "distance: "+distance);
			if(distance >= MIN_DISTANCE){
				gList.add(new GeoPoint((int)(dLat*1E6), (int)(dLon*1E6)));
				prevLocation.set(currLocation);
			}
		}

	}

	/**
	 * Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )
	 */
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		//currentElement = false;
	}
	
	/**
	 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
	 * AndroidPeople Character )
	 */
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		/*
		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}
		*/
	}
	
}
